<template>
  <div style="height: 100%;background-color:white ;">
    <el-row>
      <el-col :span="6">
        <el-button @click="selectedSync" size="small" type="success">批量推送
        </el-button>
        <el-button @click="oneKeySync" size="small" type="success">一键推送
        </el-button>
      </el-col>
      <el-col :span="18">
        <PushSearch :bookSubTagList="bookSubTagList" ref="pushSearch" @advanceConfirm="advanceConfirm"></PushSearch>
      </el-col>
    </el-row>
    <el-row>
      <el-table highlight-current-row :data="officialTableData" border style="width: 100%;" stripe
                @selection-change="handleSelectionChange" :height="tableHeight">
        <el-table-column
          v-model="multipleSelection"
          type="selection"
          width="55">
        </el-table-column>
        <el-table-column label="图书名称" min-width="350" prop="name">
          <template slot-scope="scope">
            <span v-html="scope.row.name"></span>
          </template>
        </el-table-column>
        <el-table-column label="图书类型" min-width="120" prop="name">
          <template slot-scope="scope">
            <span v-html="CONST.BOOK_TYPE.find(item => item.value === scope.row.bookType).label"></span>
          </template>
        </el-table-column>
        <el-table-column label="图书分类" min-width="150" prop="categoryStr"></el-table-column>
        <el-table-column label="书号" min-width="150" prop="isbn"></el-table-column>
        <el-table-column label="推送时间" min-width="150" prop="operatedTime"></el-table-column>
        <el-table-column label="版本号" min-width="150" prop="version"></el-table-column>
        <el-table-column label="推送状态" min-width="100" prop="syncStatus">
          <template slot-scope="scope">
            <span v-if="scope.row.syncStatus === 0" style="color: red">推送失败</span>
            <span v-else-if="scope.row.syncStatus === 1" style="color: red">消费失败</span>
            <span v-else-if="scope.row.syncStatus === 3">推送成功</span>
            <span v-else style="color: #FF8C00">待推送</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="280">
          <template slot-scope="scope">
            <span v-if="scope.row.bookType !== 2"><el-link @click.prevent="detail(scope.row.id)">详情</el-link></span>
            <span v-if="scope.row.bookType !== 2"><el-link
              @click.prevent="showMetadata(scope.row.id,scope.$index)">元数据</el-link></span>
            <span v-if="scope.row.bookType !== 0"><el-link
              @click.prevent="showIncreaseMetadata(scope.row.id,scope.$index)">增值元数据</el-link></span>
            <span><el-link @click.prevent="syncSingle(scope.row.id)">推送</el-link></span>
            <span><el-link @click.prevent="logShow(scope.row.id,scope.row.name)">查看日志</el-link></span>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <el-row>
      <div>
        <el-pagination
          :total="totalNum"
          :page-size="pageSize"
          :page-sizes="[5, 10, 15, 20, 50, 100]"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
          background
          layout="total, sizes, prev, pager, next, jumper"
          v-if="multiplePages"
          style="text-align: right;"></el-pagination>
      </div>
    </el-row>
  </div>
</template>

<script>
import PushSearch from "./PushSearch";

export default {
  components: {
    PushSearch
  },
  data() {
    return {
      tableRowStyle: '',
      tableHeight: '',
      officialTableData: [],
      multiplePages: true,
      noData: true,
      totalNum: 0,
      currentPage: 1, // 第几页
      pageSize: 10, // 每页显示的数据个数
      bookSubTagList: [],
      multipleSelection: []
    }
  },
  methods: {
    //分页
    handleCurrentChange: function (val) {
      this.currentPage = val;
      this.advanceConfirm();
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection;
    },
    logShow: function (id, name) {
      this.$router.push({
          path: '/bookLog',
          query: {id: id, name: name}
        }
      )
    },
    showMetadata: function (id, index) {
      this.$router.push({
          path: '/showMetadata',
          query: {
            id: id,
            metadata: this.officialTableData[index].metadata,
            categoryStr: this.officialTableData[index].categoryStr
          }
        }
      )
    },
    showIncreaseMetadata: function (id, index) {
      this.$router.push({
          path: '/showIncreaseMetadata',
          query: {
            formData: this.officialTableData[index].metadata.increase
          }
        }
      )
    },
    detail: function (id) {
      this.$router.push({
          path: '/bookDetail',
          query: {id: id}
        }
      )
    },
    // 单条推送
    syncSingle(id) {
      this.syncBook(id);
    },
    // 多选推送
    selectedSync() {
      if (this.multipleSelection.length <= 0) {
        this.$message.warning("请至少选择一本图书推送")
        return;
      }
      let ids = this.multipleSelection.map(item => item.id).join(",");
      this.syncBook(ids);
    },
    // 一键推送
    oneKeySync() {
      this.syncBook('');
    },
    syncBook(ids) {
      if (UTIL.isEmpty(this.$refs.pushSearch.bindingPlatform)) {
        return;
      }
      this.axios.get('/pmph/action/book/syncBook', {
        params: {
          ids: ids,
          name: this.$refs.pushSearch.name,
          category: JSON.stringify(this.$refs.pushSearch.category),
          updateTime: UTIL.isNotEmpty(this.$refs.pushSearch.updateTime) ? this.$refs.pushSearch.updateTime.toString() : '',
          ISBN: this.$refs.pushSearch.ISBN,
          syncStatus: this.$refs.pushSearch.syncStatus,
          bindingPlatform: this.$refs.pushSearch.bindingPlatform,
        }
      })
    },
    handleSizeChange(value) {
      this.pageSize = value;
      this.advanceConfirm();
    },
    advanceConfirm: function (isFromChild) {
      if (UTIL.isNotEmpty(isFromChild)) {
        this.currentPage = 1;
      }
      if (UTIL.isEmpty(this.$refs.pushSearch.bindingPlatform)) {
        return;
      }
      this.axios.get('/pmph/action/book/selectOfficialList', {
        params: {
          currentPage: this.currentPage, // 第几页
          pageNum: this.pageSize, // 每页显示的数据个数
          name: this.$refs.pushSearch.name,
          category: JSON.stringify(this.$refs.pushSearch.category),
          updateTime: UTIL.isNotEmpty(this.$refs.pushSearch.updateTime) ? this.$refs.pushSearch.updateTime.toString() : '',
          ISBN: this.$refs.pushSearch.ISBN,
          syncStatus: this.$refs.pushSearch.syncStatus,
          bindingPlatform: this.$refs.pushSearch.bindingPlatform,
          version:this.$refs.pushSearch.version
        }
      }).then((response) => {
        this.noData = response.data.totalNum <= 0;
        this.officialTableData = response.data.records
        this.totalNum = response.data.total;
      }).catch((error) => {
        console.error(error);
        this.tableData = [];
        this.noData = true;
        this.$message.error("未能请求到数据");
      })
    },
  },
  created() {
    this.tableHeight = CONST.WORKSPACE_HEIGHT - 140;
    this.axios.get('/pmph/action/tag/selectBookSubTagTree').then((response) => {
      this.bookSubTagList = response.data
    })
  },
  watch: {
    officialTableData(val) {
      for (let officialBook of val) {
        officialBook.categoryStr = []
        if (Array.isArray(officialBook.category)) {
          let cntTagList = this.bookSubTagList
          for (let item of officialBook.category) {
            let cntTag = cntTagList.find(tag => tag.id === item)
            officialBook.categoryStr.push(cntTag.name)
            cntTagList = cntTag.children
          }
        }
        officialBook.categoryStr = officialBook.categoryStr.join(" / ")
      }
    }
  },
}
</script>

<style scoped>

</style>
